using Godot;

public class CraftSlot : TextureButton
{
    public int index = 0;
    public delegate void MouseEvent(CraftSlot craftSlot);
    public MouseEvent MouseEnterEvent;
    public MouseEvent MouseExitEvent;
    public BackPack backPack;
    public Player player;
    public Recipe recipe;
    public Sprite sprite, ItemTexture;
    public override void _Ready()
    {
        sprite = GetNode<Sprite>("CraftSlotIco");
        ItemTexture = GetNode<Sprite>("ItemTexture");
    }
    public void SetRecipe(Recipe recipe)
    {
        this.recipe = recipe;
        if (recipe == null)
        {
            ItemTexture.SetTexture(null);
        }
        else
        {
            ItemTexture.SetTexture(recipe.Target[0].getMaterial().texture);
        }
    }
    public void MouseEnter()
    {
        if (MouseEnterEvent != null) MouseEnterEvent(this);
    }
    public void MouseExit()
    {
        if (MouseExitEvent != null) MouseExitEvent(this);
    }
    public void OnClick()
    {
        if (recipe == null) return;
        if (recipe.CanCraft(BlockWorld.player.store))
        {
            recipe.RemoveRes(BlockWorld.player.store);
            recipe.GiveItem(BlockWorld.player.store);
            BlockWorld.player.store.lastIndex = index;
            BlockWorld.player.store.lastRecipe = recipe;
        }
    }
    public void SetFrame(int f)
    {
        sprite.SetFrame(f);
    }
}
